package com.newidea.jiangong.component;

import com.newidea.jiangong.constant.HeatCons;
import com.newidea.jiangong.service.IndoorTemMonitorHeadService;
import com.newidea.jiangong.service.MachineMonitorService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class HeatScheduled {

    /**
     * 北京市智能供热管理云平台：大屯
     */
    @Value("${collect.login.datun.account}")
    private String datun_account;
    @Value("${collect.login.datun.password}")
    private String datun_password;
    /**
     * 北京市智能供热管理云平台：建邦枫景
     */
    @Value("${collect.login.jbfj.account}")
    private String jbfj_account;
    @Value("${collect.login.jbfj.password}")
    private String jbfj_password;

    private final HeatComponent heatComponent;
    private final MachineMonitorService machineMonitorService;
    private final IndoorTemMonitorHeadService indoorTemMonitorHeadService;


    public HeatScheduled(
        HeatComponent heatComponent, MachineMonitorService machineMonitorService,
        IndoorTemMonitorHeadService indoorTemMonitorHeadService
    ) {
        this.heatComponent = heatComponent;
        this.machineMonitorService = machineMonitorService;
        this.indoorTemMonitorHeadService = indoorTemMonitorHeadService;
    }


    /**
     * 每小时0分和30分时各执行一次
     */
    @Scheduled(cron = "0 0/30 * * * *")
    @Transactional(rollbackFor = Exception.class)
    public void reviseCycle() {
        try {

            // ----- 采集：大屯  -----
            String datunToken = heatComponent.getToken(datun_account, datun_password);
            // 采集机房监控数据
            machineMonitorService.collect(HeatCons.DA_TUN, datunToken);
            // 采集室温监控数据和室温监控分析数据
            indoorTemMonitorHeadService.collect(HeatCons.DA_TUN, datunToken);


            // ----- 采集：建邦枫景  -----
            String jbfjToken = heatComponent.getToken(jbfj_account, jbfj_password);
            // 采集室温监控数据和室温监控分析数据
            indoorTemMonitorHeadService.collect(HeatCons.J_B_F_J, jbfjToken);

        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}
